﻿@using FujiyBlog.Core.DomainObjects
@using FujiyBlog.Web.Common
@using FujiyBlog.Core.Extensions

@model FujiyBlog.Web.Areas.Admin.ViewModels.AdminPageIndex

@{
    Layout = MVC.Admin.Page.Views._Layout;
    ViewBag.Title = Resources.Labels.Pages;
}

<h2>@Resources.Labels.Pages</h2>

@if (User.IsInRole(Role.CreateNewPages))
{
    <p>
        @Html.ActionLink("Add new page", MVC.Admin.Page.Edit())
    </p>
}

<div id="pages-type-filters">
@Html.ActionLink("All", MVC.Admin.Page.Index()) | @Html.ActionLink("Drafts", MVC.Admin.Page.Index(null, false))| @Html.ActionLink("Published", MVC.Admin.Page.Index(null, true))
</div>

@Pager.Links(Model.CurrentPage, Model.TotalPages, Resources.Labels.NextPage, Resources.Labels.PreviousPage)

<table>
    <tr>
        <th>
            Published
        </th>
        <th>
            Title
        </th>
        <th>
            Show In List
        </th>
        <th>
            CreationDate
        </th>
        <th>
            PublicationDate
        </th>
        <th></th>
    </tr>

@foreach (var item in Model.Pages)
{
    <tr id="item-row-@item.Id">
        <td class="@(item.IsPublished ? "published" : "notpublished")">
            @Html.DisplayFor(modelItem => item.IsPublished)
        </td>
        <td>
            @Html.ActionLink(item.Title, MVC.Page.Details(item.Slug))
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ShowInList)
        </td>
        <td>
            @DateTimeUtil.ConvertUtcToMyTimeZone(item.CreationDate)
        </td>
        <td>
            @DateTimeUtil.ConvertUtcToMyTimeZone(item.PublicationDate)
        </td>
        <td>
            @if ((item.Author.Username == User.Identity.Name && User.IsInRole(Role.EditOwnPages)) || User.IsInRole(Role.EditOtherUsersPages))
            {
                @Html.ActionLink("Edit", MVC.Admin.Page.Edit(item.Id))
            }
            @if ((item.Author.Username == User.Identity.Name && User.IsInRole(Role.DeleteOwnPages)) || (item.Author.Username != User.Identity.Name && User.IsInRole(Role.DeleteOtherUsersPages)))
            {
                <text>|
                @Ajax.ActionLink("Delete", MVC.Admin.Page.Delete(item.Id), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "RemoveItemRow(" + item.Id + ");" })
                </text>
            }
        </td>
    </tr>
}
</table>

<script type="text/javascript">
    function RemoveItemRow(itemId) {
        $('#item-row-' + itemId).hide();
    }
    
</script>

@Pager.Links(Model.CurrentPage, Model.TotalPages, Resources.Labels.NextPage, Resources.Labels.PreviousPage)
